import { BookData } from "./interfaces";
import { OPDSEntry } from "opds-feed-parser";

/** Extract metadata and links from an OPDS entry for use on the
    book details page. */
export default function adapter(data: OPDSEntry): BookData {
  
  console.log('editorAdapter OPDSEntry data:',data)

  const hideLink = data.links.find((link) => {
    return link.rel === "http://librarysimplified.org/terms/rel/hide";
  });

  const restoreLink = data.links.find((link) => {
    return link.rel === "http://librarysimplified.org/terms/rel/restore";
  });

  const refreshLink = data.links.find((link) => {
    return link.rel === "http://librarysimplified.org/terms/rel/refresh";
  });

  const editLink = data.links.find((link) => {
    return link.rel === "edit";
  });

  const issuesLink = data.links.find((link) => {
    return link.rel === "issues";
  });

  const changeCoverLink = data.links.find((link) => {
    return link.rel === "http://librarysimplified.org/terms/rel/change_cover";
  });

  const audience = data.categories.find((category) => {
    return category.scheme === "http://schema.org/audience";
  });

  const targetAgeCategory = data.categories.find((category) => {
    return category.scheme === "http://schema.org/typicalAgeRange";
  });
  let targetAgeRange = [];
  if (targetAgeCategory && /\d\d?-?\d?\d?/.test(targetAgeCategory.term)) {
    targetAgeRange = targetAgeCategory.term.split("-");
  }

  const fictionCategory = data.categories.find((category) => {
    return category.scheme === "http://librarysimplified.org/terms/fiction/";
  });
  let fiction;
  if (fictionCategory) {
    fiction = fictionCategory.label === "Fiction";
  }

  const categories = data.categories.map((category) => category.label);

  let medium;
  try {
    medium = data.unparsed["$"]["schema:additionalType"]["value"];
  } catch (e) {
    medium = null;
  }

  let imprint;
  try {
    imprint = data.unparsed["bib:publisherImprint"][0]["_"];
  } catch (e) {
    imprint = null;
  }

  const authors = [];
  for (const author of data.authors) {
    authors.push(Object.assign({}, author, { role: "aut" }));
  }
  console.log('电子书原始数据',data)

  let rating;
  try {
    const ratings = data.unparsed["schema:Rating"];
    for (const ratingTag of ratings) {
      if (
        ratingTag["$"]["schema:additionalType"]["value"] ===
        "http://schema.org/ratingValue"
      ) {
        rating = ratingTag["$"]["schema:ratingValue"]["value"];
        break;
      }
    }
  } catch (e) {
    rating = null;
  }

  const imageLink = data.links.find((link) => {
    return link.rel === "http://opds-spec.org/image";
  });
  let coverUrl;
  if (imageLink) {
    coverUrl = imageLink.href;
  }

  return {
    id: data.id,
    title: data.title,
    licensepoolsId: data.licensepoolsId,
    // licensepoolsId: '9527',
    authors: authors,
    contributors: data.contributors,
    subtitle: data.subtitle,
    summary: data.summary.content,
    audience: audience && audience.term,
    targetAgeRange: targetAgeRange,
    fiction: fiction,
    categories: categories,
    hideLink: hideLink,
    restoreLink: restoreLink,
    refreshLink: refreshLink,
    editLink: editLink,
    issuesLink: issuesLink,
    changeCoverLink: changeCoverLink,
    series: data.series && data.series.name,
    seriesPosition: data.series && data.series.position,
    medium: medium,
    language: data.language,
    publisher: data.publisher,
    imprint: imprint,
    issued: data.issued,
    rating: rating,
    coverUrl: coverUrl,
  };
}
